package com.fengling.controller;

import com.fengling.pojo.Dept;
import com.fengling.pojo.Result;
import com.fengling.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/depts")


public class DeptController {
    // private Logger log= LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;


    @GetMapping
    public Result selectD() {
        log.info("查询全部部门数据");
        List<Dept> depts = deptService.select();

        return Result.success(depts);
    }

    @DeleteMapping("/{id}")
    private Result deleteD(@PathVariable Integer id)throws Exception{
        log.info("根据id删除部门："+id);
        deptService.delete(id);
        return Result.success();

    }

    @PostMapping
    public Result insertD(@RequestBody Dept dept){
        log.info("新建部门："+dept.getName());


        deptService.insert(dept);
        return Result.success();


    }
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("准备修改："+id+"这个部门");
        Dept dept=deptService.find(id);
        return Result.success(dept);


    }


    @PutMapping
    public Result changeById(@RequestBody Dept dept){
        log.info("修改为"+dept.getName());
        deptService.changeById(dept);
        return Result.success();

    }
}
